﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DeltaFlex.Negoci;

using Negoci = DeltaFlex.Negoci;

namespace Vista.Controls
{
    public partial class AltaMaquina : System.Web.UI.UserControl
    {
        #region Variables Globals
        public string NomMaquina
        {
            get
            {
                return (Convert.ToString(ViewState["_nomMaquina"]) == string.Empty ?
                    "-" : Convert.ToString(ViewState["_nomMaquina"]));
            }
            set { ViewState["_nomMaquina"] = value; }
        }
        
        public Client Client
        {
            get
            {
                object result = Session["_client"];
                if (result == null) result = null;
                return (Client)result;
            }
            set { Session["_client"] = value; }
        }

        public string Deflaccio
        {
            get
            {
                return (Convert.ToString(ViewState["_deflaccio"]) == string.Empty ?
                    "-" : Convert.ToString(ViewState["_deflaccio"]));
            }
            set { ViewState["_deflaccio"] = value; }
        }

        public string Calibre
        {
            get
            {
                return (Convert.ToString(ViewState["_calibre"]) == string.Empty ?
                    "-" : Convert.ToString(ViewState["_calibre"]));
            }
            set { ViewState["_calibre"] = value; }
        }

        public string URLFitxaMaquina
        {
            get
            {
                return (Convert.ToString(ViewState["_urlFitxaMaquina"]) == string.Empty ?
                    "-" : Convert.ToString(ViewState["_urlFitxaMaquina"]));
            }
            set { ViewState["_urlFitxaMaquina"] = value; }
        }

        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            

        }
        #region Carrega a memoria
        /// <summary>
        /// Carrega el contingut dels text Box del control Alta Màquina
        /// </summary>
        private void CarregarMemoria()
        {
            int idClient = Convert.ToInt32(ddlClient.SelectedValue);

            Client = Negoci.Client.LlistarClientByIDClient(idClient)[0];
            NomMaquina = txtNomMaquina.Text;
            Deflaccio = txtDeflacció.Text;
            Calibre = txtCalibre.Text;
            URLFitxaMaquina = txtUrlFitxaMaquina.Text;
        }
        #endregion

        #region Neteja Controls
        /// <summary>
        ///Neteja tots els controls del formulari.
        /// </summary>
        /// <param name="controles"></param>
        private void NetejaControls(ControlCollection controles)
        {
            foreach (Control control in controles)
            {
                if (control is TextBox)
                    ((TextBox)control).Text = string.Empty;
                else if (control is DropDownList)
                    ((DropDownList)control).ClearSelection();
                else if (control is RadioButtonList)
                    ((RadioButtonList)control).ClearSelection();
                else if (control is CheckBoxList)
                    ((CheckBoxList)control).ClearSelection();
                else if (control is RadioButton)
                    ((RadioButton)control).Checked = false;
                else if (control is CheckBox)
                    ((CheckBox)control).Checked = false;
                else if (control.HasControls())
                    //Esta linea detecta un Control que contigui altres Controls
                    //Així cap control es quedarà sense netejar
                    NetejaControls(control.Controls);
            }
        }
        #endregion

        #region Guardar
        protected void btnAltaMaquina_Click(object sender, EventArgs e)
        {
            string missatge, exepcio;
            missatge = exepcio = string.Empty;

            CarregarMemoria();

            try
            {
                MaquinaCartoner.InsertarMaquina(Client.IdClient, NomMaquina, Deflaccio, Calibre, URLFitxaMaquina);
            }

            catch (Exception ex)
            {
                exepcio = "Error en donar d'alta un nou client <br/> " + ex.Message;
            }

            if (String.IsNullOrEmpty(exepcio))
                missatge = ConstantsAltes.mplbGuardarMaquinaOK;
            else
                missatge = ConstantsAltes.mplbGuardarMaquinaKO + " <br/> " + exepcio;

            mpAltaMaquina.LabelHeader = ConstantsAltes.mplhGuardarMaquina;
            mpAltaMaquina.LabelBody = missatge;
            mpAltaMaquina.MostraInfo = true;

            NetejaControls(this.Controls);

            mpAltaMaquina.MostrarModal();
        }
        #endregion
    }
}